home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 191_01 / sound.c < prev    next >
Text File  |  1986-05-23  |  3KB  |  117 lines

  1.  
  2. /*  SOUND.C                             Last update: 26 Feb 86  */
  3.  
  4. /* ------------------------------------------------------------ */
  5. /*      This is a portion of the SOUND EFFECTS LIBRARY.         */
  6. /*                                                              */
  7. /*      Copyright (C) 1986 by Paul Canniff.                     */
  8. /*      All rights reserved.                                    */
  9. /*                                                              */
  10. /*      This library has been placed into the public domain     */
  11. /*      by the author.  Use is granted for non-commercial       */
  12. /*      pusposes, or as an IMBEDDED PORTION of a commercial     */
  13. /*      product.                                                */
  14. /*                                                              */
  15. /*      Paul Canniff                                            */
  16. /*      PO Box 1056                                             */
  17. /*      Marlton, NJ 08053                                       */
  18. /*                                                              */
  19. /*      CompuServe ID: 73047,3715                               */
  20. /*                                                              */
  21. /* ------------------------------------------------------------ */
  22.  
  23. #define DEBUG 0
  24.  
  25. #include <stdio.h>
  26. #include "sound.h"
  27.  
  28.  
  29. extern struct raw_sound soundbuff[SND_BUFFSIZE];
  30. extern unsigned sb_inptr, sb_outptr;
  31.  
  32.  
  33. int sound(f,d)
  34. long f;
  35. unsigned d;
  36. {
  37.     int mode;
  38.     unsigned np;
  39.  
  40.     if ((mode = sound_mode()) == 1)
  41.     {
  42. #if DEBUG
  43.         printf("FG\n");
  44. #endif
  45.         if (f == 0)
  46.         {
  47.             spkr_off();
  48.             delay(d);
  49.             return 0;
  50.         }
  51.         if (spkr_freq(f) == -1)
  52.             return -1;
  53.         else
  54.         {
  55.             spkr_on();
  56.             delay(d);
  57.             spkr_off();
  58.             return 0;
  59.         }
  60.     }
  61.     else
  62.     {
  63. #if DEBUG
  64.         printf("BG i=%d o=%d\n",sb_inptr,sb_outptr);
  65. #endif
  66.  
  67.         if ((np = ((sb_inptr + 1) % SND_BUFFSIZE)) == sb_outptr)
  68.             return -1;  /* Buffer overflow */
  69.  
  70. #if DEBUG
  71.         printf("Sound duration is %d msec or %d ticks\n",d,MS2TICKS(d));
  72. #endif
  73.         soundbuff[np].count = FREQ2CTR(f);
  74.         soundbuff[np].ticks = MS2TICKS(d);
  75.         sb_inptr = np;
  76.         if (mode <= 3)
  77.             return bwait();
  78.         else
  79.             return 0;
  80.     }
  81. }
  82.  
  83.  
  84. int sounds(n,elem)
  85. int n;
  86. struct sound_element *elem;
  87. {
  88.     while (n--)
  89.         if (sound(elem->freq,elem->dur) == -1)
  90.             return -1;
  91.  
  92.     return 0;
  93. }
  94.  
  95.  
  96.     /* BWAIT waits a while until the buffer empties. */
  97.     /*  (It has a timeout counter and will return -1 */
  98.     /*   if it times out before the buffer empties)  */
  99.  
  100. static int bwait()
  101. {
  102.     register int i = MAXINT;
  103.  
  104.     while (sb_inptr != sb_outptr)
  105.     {
  106.         if (--i == 0) return -1;
  107.          else         dummy();      /* Force no aliasing */
  108.     }
  109.  
  110.     return 0;
  111. }
  112.  
  113. static int dummy()
  114. {
  115.     return 0;
  116. }
  117.